package com.tycmc.common.util;

import org.apache.storm.shade.org.apache.commons.collections.MapUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListUtil {
    public static Map<String, List<Map<String, Object>>> groupBy(String key1, List<Map<String, Object>> srcList) {
        Map<String, List<Map<String, Object>>> retMap = new HashMap<>();
        String mapKey;
        List<Map<String, Object>> mapList;
        if (null != srcList && srcList.size() > 0) {
            for (Map<String, Object> map : srcList) {
                mapKey = MapUtils.getString(map, key1);
                mapList = retMap.get(mapKey);
                if (mapList == null) {
                    mapList = new ArrayList<>();
                    mapList.add(map);
                    retMap.put(mapKey, mapList);
                } else {
                    mapList.add(map);
                }
            }
        }
        return retMap;
    }
}
